package com.weitu.train.plan.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.train.plan.entity.Train;
import com.weitu.train.plan.service.TrainService;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.weitu.util.AjaxResult;
import org.springframework.web.servlet.ModelAndView;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;
import com.weitu.util.TableTreeNodeUtil;
/**
 * Created by WT-SUN on 2017/5/8.
 */
@Controller
@RequestMapping(value = "train/plan")
public class TrainController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "train/plan/";

    @Autowired
    private TrainService trainService;

    @Autowired
    private AccessoryUtilService accessoryUtilService;

    @RequestMapping(value = "/list")
    public String list() {
        return MODULE_BATH_PATH + "list";
    }

    @RequestMapping(value = "/add")
    public String add() {
        return MODULE_BATH_PATH + "add";
    }

    @RequestMapping(value = "/listTb")
    public String listTb() {
        return MODULE_BATH_PATH + "listTb";
    }

    /**
     * 统计分析 按班
     * @return
     */
    @RequestMapping(value = "/a/list")
    public String alist() {
        return MODULE_BATH_PATH + "analysis";
    }
    /**
     * 统计分析 按人
     * @return
     */
    @RequestMapping(value = "/b/list")
    public String blist() {
        return MODULE_BATH_PATH + "aPerson";
    }

    @ResponseBody
    @RequestMapping("insertData")
    @SystemLog(module = "培训管理——培训计划", methods = "新增培训计划")
    @Transactional
    public AjaxResult insertData(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState",0);
        String planId=paramMap.get("id").toString();
        boolean accFlag= AccessoryUtil.saveAccerssoryInfo(planId);
        boolean boo=trainService.insertSelective(paramMap);
        if (boo) {
            return success("添加成功");
        }else {
            return fail("添加失败");
        }
    }

    /**
     * 更新培训计划
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "培训管理——培训计划", methods = "更新培训计划")
    public AjaxResult updateByIdSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getEditBasicFieldMap(paramMap);
        boolean boo=trainService.updateByIdSelective(paramMap);
        if (boo) {
            boolean accFlag= AccessoryUtil.saveAccerssoryInfo(paramMap.get("id").toString());
            if (accFlag){
                return success("更新成功");
            }else {
                return fail("更新失败");
            }
        } else {
            return fail("更新失败");
        }
    }
    /**
     * 弹窗列表及培训计划页面列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/TrTable", method = RequestMethod.POST)
    public Object TrTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=trainService.selectAllPlansCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Train> trains =trainService.selectAllPlans(paramMap);
        Object data=dataGridPagination.flushDataGridData(trains);
        return data;
    }

    /**
     * 计划记录列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/TrainTable", method = RequestMethod.POST)
    public Object TrainTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=trainService.selectAllListPlansCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Train> maps=trainService.selectListPlans(paramMap);
        Object data=dataGridPagination.flushDataGridData(maps);
        return data;
    }

    /**
     * 编辑与详情页面
     * @param id
     * @param type
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(String id, String type) {
        ModelAndView modelAndView = new ModelAndView();
        Train train=trainService.selectById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        modelAndView.addObject("tr", train);
        modelAndView.addObject("accessoryList",accessoryList);
        if (type.equals("info")) {
            modelAndView.setViewName(MODULE_BATH_PATH + "info");
        } else {
            modelAndView.setViewName(MODULE_BATH_PATH + "edit");
        }
        return modelAndView;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "培训管理——培训计划", methods = "删除培训计划")
    public AjaxResult deleteById(String id){
        int boo=trainService.deleteById(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    /**
     * 统计分析 按班次
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/selectAnalysis", method = RequestMethod.POST)
    public List<Map<String,Object>> selectAnalysis(HttpServletRequest request) throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        List<Map<String,Object>> maps=trainService.selectAnalysis(paramMap);
        List<Map<String,Object>> mapList=TableTreeNodeUtil.getFatherNode(maps);
        return mapList;
    }
    /**
     * 统计分析 按人
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/selectAnalysisPerson", method = RequestMethod.POST)
    public List<Map<String,Object>> selectAnalysisPerson(HttpServletRequest request) throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        List<Map<String,Object>> maps=trainService.selectAnalysisPerson(paramMap);
        List<Map<String,Object>> mapList=TableTreeNodeUtil.getFatherNode(maps);
        return mapList;
    }
}
